package com.zfast.forge.common;

import org.apache.ibatis.mapping.MappedStatement;
import tk.mybatis.mapper.entity.EntityColumn;
import tk.mybatis.mapper.mapperhelper.EntityHelper;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.mapper.mapperhelper.MapperTemplate;
import tk.mybatis.mapper.mapperhelper.SqlHelper;

import java.util.Iterator;
import java.util.Set;

public class SqlProvider extends MapperTemplate {
    public SqlProvider(Class<?> mapperClass, MapperHelper mapperHelper) {
        super(mapperClass, mapperHelper);
    }

    public String get(MappedStatement ms) {
        Class<?> entityClass = this.getEntityClass(ms);
        this.setResultType(ms, entityClass);
        StringBuilder sql = new StringBuilder();
        sql.append(SqlHelper.selectAllColumns(entityClass));
        sql.append(SqlHelper.fromTable(entityClass, this.tableName(entityClass)));
        sql.append(SqlHelper.wherePKColumns(entityClass));
        System.out.println(sql.toString());
        return sql.toString();
    }

    public String disable(MappedStatement ms) {
        Class<?> entityClass = this.getEntityClass(ms);
        Set<EntityColumn> entityColumns = EntityHelper.getPKColumns(entityClass);
        StringBuilder sql = new StringBuilder();
        sql.append(SqlHelper.updateTable(entityClass, this.tableName(entityClass)));
        Iterator var5 = entityColumns.iterator();

        while (var5.hasNext()) {
            EntityColumn column = (EntityColumn) var5.next();
            if (column.isId()) {
                sql.append("\n SET enabled_flag = 0");
                sql.append("\n    WHERE " + column.getColumn() + " IN ");
                sql.append("\n    <foreach collection=\"array\" item=\"id\" open=\"(\" separator=\",\" close=\")\">");
                sql.append("\n        #{id}");
                sql.append("\n    </foreach>");
                break;
            }
        }

        return sql.toString();
    }
}

